<?php


namespace app\app\controller;


use app\common\util\Pinyin;

class Broadcast extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function dealM3u()
    {
        $timeStart = microtime(true);
        $filePath = "/Users/hainuo/Downloads/iptv-checker-30-12-2020-04-02-02/online.m3u";
        $content = file_get_contents($filePath);
        $content = explode("\n",$content);
        $content = array_filter($content);
        selfDump($content);
        $broadList = [];
        $i = 0;
        foreach ($content as $index=>$broad){
            if(empty($broad)){
                continue;
            }
            dump('zhengzai循环'.$index);
            if(strpos($broad,'#EXTINF:-1 ,')!==false){
                if(!isset($broadList[$i])){
                    $broadList[$i]=[];
                }
                $vodName = trim(str_replace('#EXTINF:-1 ,','',$broad));
                dump('vodName'.$vodName);
                if($i>0 && $broadList[$i-1]['vod_name']==$vodName){
                    dump(22222);
                    $i--;
                }

                $broadList[$i]['vod_name']=$vodName;
            }elseif (strpos($broad,'http')!==false ){
                if(!isset($broadList[$i]['vod_play_url'])){
                    $broadList[$i]['vod_play_url']=[];
                }
                $broadList[$i]['vod_play_url'][]=trim($broad);
                $i++;
            }
        }
        $broadList = array_filter($broadList);

        foreach ($broadList as &$item){
            foreach ($item['vod_play_url'] as $key=>&$url){
                $url='线路'.($key+1).'$'.$url;
            }
            $item['vod_play_url'] = join('#',$item['vod_play_url']);
            $item['vod_en'] = Pinyin::get($item['vod_name']);
            $item['vod_pic'] = '';
            $item['vod_play_from']='ctm3u8';
            $item['vod_play_server']='no';
            $item['vod_score']=0;
            $item['vod_content']=$item['vod_name'];
            $item['vod_down_url']='';
            $item['vod_plot_name']='';
            $item['vod_plot_detail']='';
            $item['vod_area']='中国';
            $item['vod_status'] = 1;
            $item['type_id'] = 42;
            $item['type_id_1'] = 40;
            $item['vod_serial']='';
            $item['vod_time']=$timeStart;
            db('vod')->insertGetId($item);
        }
        selfDump($broadList);
    }
}